import { request } from '../utils/request';
import type { 
  ScanTaskInfo, 
  TaskRecord, 
  ScanTaskQueryParams, 
  PageResult,
  ApiResponse 
} from '../pages/UnitDetail/ScanRecords/types';

// 分页查询扫描任务
export async function fetchScanTaskList(params: ScanTaskQueryParams): Promise<PageResult<ScanTaskInfo>> {
  const response = await request({ 
    url: '/unit/scantask/pageQuery', 
    method: 'post', 
    data: params 
  });
  
  if (response.code === 0) {
    return response.data;
  }
  throw new Error(response.msg || response.message || '查询扫描任务失败');
}

// 根据扫描任务ID查询任务记录
export async function fetchTaskRecords(unitScanTaskId: number): Promise<TaskRecord[]> {
  const response = await request({ 
    url: `/task/records/unit-scan-task/${unitScanTaskId}`, 
    method: 'get'
  });
  
  if (response.code === 0) {
    return response.data || [];
  }
  throw new Error(response.msg || response.message || '查询任务记录失败');
}

// 根据任务ID查询扫描任务详情
export async function fetchScanTaskDetail(unitTaskId: number): Promise<ScanTaskInfo> {
  const response = await request({ 
    url: `/unit/scantask/${unitTaskId}`, 
    method: 'get'
  });
  
  if (response.code === 0) {
    return response.data;
  }
  throw new Error(response.msg || response.message || '查询扫描任务详情失败');
}

// 删除扫描任务
export async function deleteScanTask(unitTaskId: number): Promise<boolean> {
  const response = await request({ 
    url: `/unit/scantask/${unitTaskId}`, 
    method: 'delete'
  });
  
  if (response.code === 0) {
    return response.data;
  }
  throw new Error(response.msg || response.message || '删除扫描任务失败');
}

// 获取扫描任务详细结果
export async function fetchDetailedResult(unitScanTaskId: number): Promise<string> {
  const response = await request({ 
    url: `/task/records/detailed-result/${unitScanTaskId}`, 
    method: 'get'
  });
  
  if (response.code === 0) {
    return response.data || '';
  }
  throw new Error(response.msg || response.message || '查询详细结果失败');
}